﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace LifeTree.Mvc.Extensions
{
    public static class ModelStateDictionaryExtensions
    {
        public static object GetErrors(this ModelStateDictionary modelStateDictionary)
        {
            var result = new List<object>();
            foreach (var key in modelStateDictionary.Keys)
            {
                if (modelStateDictionary[key].Errors.Count > 0)
                {
                    List<string> messages = new List<string>();
                    foreach (var item in modelStateDictionary[key].Errors)
                    {
                        messages.Add(item.ErrorMessage);
                    }
                    result.Add(new { control = key, messages = messages });
                }
            }
            return new { errors = result };
        }
    }
}
